package com.carpedil.client.service.impl;

import com.carpedil.client.service.ClienService;
import com.carpedil.client.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/** @author Administrator */
@Service
public class ClientServiceImpl implements ClienService {

  @Autowired RestTemplate restTemplate;

  @Override
  public String booking(String movieName) {
    String s = hasMovie(movieName);
    if ("1".equals(s)) {
      String orderId = String.valueOf(System.currentTimeMillis());
      String url = "http://localhost:8003/order/" + orderId + "/" + movieName;
      return restTemplate.getForEntity(url, String.class).getBody();
    } else if ("-1".equals(s)) {
      return StringUtil.OUT_OF_STOCK;
    }
    return StringUtil.NO_RESOURCE_FOUND;
  }

  @Override
  public String query(String orderId) {
    String url = "http://localhost:8003/checkStatus/" + orderId;
    return restTemplate.getForEntity(url, String.class).getBody();
  }

  private String hasMovie(String movieName) {
    String url = "http://localhost:8002/check/" + movieName;
    return restTemplate.getForEntity(url, String.class).getBody();
  }
}
